package com.twotwoshop.service.goods.config;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJackson2MessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MQ的配置
 *
 * 配置完后并不会生效 需要去 hm-common/src/main/resources/META-INF/spring.factories 配置才会生效
 *
 */
@Configuration
@ConditionalOnClass({RabbitTemplate.class})//只有含有RabbitTemplate依赖的才会生效
public class MqConfig {

    /**
     * MQ的JSON消息转换器
     */
    @Bean
    public MessageConverter MessageConverter() {

        Jackson2JsonMessageConverter jjmc = new Jackson2JsonMessageConverter();

        //唯一的id 保证业务幂等性 为每条消息生成一个唯一的id
        jjmc.setCreateMessageIds(true);
        return jjmc;
    }


    @Bean
    public AbstractJackson2MessageConverter jsonMessageConverter() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule()); // 注册 Java 8 日期/时间模块
        return new Jackson2JsonMessageConverter(objectMapper);
    }

    /**
     * 失败消息处理策略 放入失败消息队列
     * @param rabbitTemplate
     * @return
     */
   /* @Bean
    public MessageRecoverer republishMessageRecover(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
    }*/




}